<template>
  <div class="app-container">
    <div class="flex-between">
      <el-button
        type="primary"
        @click="handleAddAdmin"
      >新增门店</el-button>
      <!-- <el-input
        v-model="search"
        class="search-main"
        placeholder="请输入姓名或手机号查询"
        prefix-icon="el-icon-search"
      /> -->
    </div>

    <el-table
      :data="departmentList"
      style="width: 100%;margin-top:30px;"
      border
    >
      <el-table-column
        align="center"
        label="ID"
        width="50"
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="门店名称"
        width="220"
      >
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        align="center"
        label="提成比例"
        width="220"
      >
        <template slot-scope="scope">
          {{ scope.row.commission }}
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope)"
          >修改</el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination
      :current-page="cur_page"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pageNum"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      style="text-align:center;margin:10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> -->

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType==='edit'?'编辑角色':'新建角色'"
    >
      <el-form
        :model="department"
        label-width="80px"
        label-position="left"
      >
        <!-- <el-form-item label="销售提成">
          <el-input
            v-model="admin.commission"
            style="width:20%"
            type="number"
          />%
        </el-form-item> -->
        <el-form-item label="门店名字">
          <el-input
            v-model="department.name"
            placeholder="请输入门店名字"
          />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button
          type="danger"
          @click="dialogVisible=false"
        >取消</el-button>
        <el-button
          type="primary"
          @click="confirmRole"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getDepartment, addDepartment, updateDepartment } from '@/api/department'
import { roleC } from '@/utils/role'
const defaultDepartment = {
  name: '',
  id: ''
}

export default {
  data() {
    return {
      department: Object.assign({}, defaultDepartment),
      departmentList: [],
      dialogVisible: false,
      dialogType: 'new',
      search: ''
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    this.getAdminBase({disable: '1'})
  },
  methods: {
    async getAdminBase(a) {
      const { data } = await getDepartment(a)
      // for (const i in data.list) {
      // }
      this.departmentList = data
    },
    handleAddAdmin() {
      this.department = Object.assign({}, defaultDepartment)
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([])
      }
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.department = deepClone(scope.row)
    },
    handleDelete({ $index, row }) {
      this.$confirm('确定要删除吗?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(row)
        row.disable = 2
        await updateDepartment(row)
        this.departmentList.splice($index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
        .catch(err => { console.error(err) })
    },
    async confirmRole() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        await updateDepartment(this.department)
      } else {
        const requestData = Object.assign({}, this.department)
        const { data } = await addDepartment(requestData)
        console.log(data)
      }
      this.getAdminBase()
      // console.log(this.admin)
      // const { name } = this.admin
      this.dialogVisible = false
      this.$notify({
        title: '提交成功',
        dangerouslyUseHTMLString: true,
        // message: `
        //     <div>姓名: ${name}</div>
        //   `,
        type: 'success'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
